//
//  WBTabBar.swift
//  微博
//
//  Created by 仲启明 on 2017/1/4.
//  Copyright © 2017年 swift. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {

    var composeClosure: (() -> ())?
    
    //初始化方法中添加子视图
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    private func setupUI(){
        
        self.addSubview(composeBtn)
        
        composeBtn.addTarget(self, action: #selector(composeBtnDidClick), for: .touchUpInside)
        
        
    }

    @objc private func composeBtnDidClick(){
        
        composeClosure?()
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = UIScreen.main.bounds.width/5
        
        let h = bounds.height
        
        
        var index = 0
        
        for subView in subviews{
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRect(x: CGFloat(index) * w, y: 0, width: w, height: h)
                
                
                index += (index == 1 ? 2 : 1)
            }
        }
        
        print(self.center)
        composeBtn.center = CGPoint(x: self.center.x, y: h*0.5)
        composeBtn.bounds.size = CGSize(width: w, height: h)
        
    }
    
    //懒加载撰写按钮
    
    lazy var composeBtn: UIButton = {
        
        let btn = UIButton(type: .custom)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return btn
    }()
}
